package com.microlecture.controller;

import com.microlecture.common.Constants;
import com.microlecture.entity.*;
import com.microlecture.service.*;
import com.microlecture.utils.LoginUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.*;

/**
 * 用户控制
 * Created by 孙志伟 on 2018/4/27.
 */
@Controller
public class PageController {

    @Autowired
    private CookbookService cookbookService;

    private void indexPageData(Model model) {
        //最新食谱
        Map<String, Object> newCookbookMap = cookbookService.selectPageByCondition(1, Constants.pageSize, null, null, Constants.ORDER_BY_CREATEDATE_DESC);
        List<Cookbook> newCookbookList = (List<Cookbook>) newCookbookMap.get(Constants.ROWS);
        cookbookDetail(newCookbookList);
        model.addAttribute("newCookbookMap", newCookbookMap);
        //随机食谱
        Map<String, Object> randomCookbookMap = cookbookService.selectPageByCondition(1, Constants.pageSize, null, null, Constants.ORDER_BY_RAND);
        cookbookDetail((List<Cookbook>) randomCookbookMap.get(Constants.ROWS));
        model.addAttribute("randomCookbookMap", randomCookbookMap);
    }

    @Autowired
    private OrderService orderService;

    @Autowired
    private CookbookDiscussService cookbookDiscussService;

    @Autowired
    private CookbookFoodService cookbookFoodService;

    @Autowired
    private FoodService foodService;

    @Autowired
    private CookbookStepService cookbookStepService;

    private void cookbookDetail(List<Cookbook> cookbookList) {
        for(Cookbook cookbook : cookbookList) {
            Integer userid = cookbook.getUserid();
            Integer cookbookid = cookbook.getId();
            //购买数量
            cookbook.setOrderNumber(
                    orderService.selectByCondition(null, cookbookid).size()
            );
            //评论数
            cookbook.setDisscussNumber(
                    cookbookDiscussService.selectByCondition(null, cookbookid).size()
            );
            //食材价格
            double foodPrice = 0.0;
            List<CookbookFood> cookbookFoods = cookbookFoodService.selectByCondition(cookbookid);
            for (CookbookFood cf : cookbookFoods) {
                Food food = foodService.selectByPrimaryKey(cf.getFoodid());
                if(food != null) {
                    foodPrice += food.getPrice().doubleValue();
                }
            }
            cookbook.setFoodPrice(foodPrice);
            //食谱步骤数
            cookbook.setStepNumber(
                    cookbookStepService.selectByCondition(cookbookid).size()
            );
        }
    }

    /**
     * 主页
     * @return
     */
    @RequestMapping("/")
    public String indexRoot(Model model) {
        indexPageData(model);
        return "index";
    }

    /**
     * 主页
     * @return
     */
    @RequestMapping("/index")
    public String index(Model model) {
        indexPageData(model);
        return "index";
    }

    /**
     * 食谱页
     * @return
     */
    @RequestMapping("/cookbook")
    public String cookbook(Model model, @RequestParam("cookbookId") Integer cookbookId) {

        //食谱基本信息
        Cookbook cookbook = cookbookService.selectByPrimaryKey(cookbookId);
        cookbook.setBrowsequantity(cookbook.getBrowsequantity() + 1);
        cookbookService.updateByPrimaryKeySelective(cookbook);
        model.addAttribute("cookbook", cookbook);

        //食谱食材
        List<CookbookFood> cookbookFoods = cookbookFoodService.selectByCondition(cookbookId);
        for(int i = 0; i < cookbookFoods.size(); i++) {
            Food food = foodService.selectByPrimaryKey(cookbookFoods.get(i).getFoodid());
            cookbookFoods.get(i).setFood(food);
        }
        model.addAttribute("cookbookFoods", cookbookFoods);
        //食谱步骤
        List<CookbookStep> cookbookSteps = cookbookStepService.selectByCondition(cookbookId);
        model.addAttribute("cookbookSteps", cookbookSteps);
        //食谱评论
        List<CookbookDiscuss> cookbookDiscusses = cookbookDiscussService.selectByCondition(null, cookbookId);
        model.addAttribute("cookbookDiscusses", cookbookDiscusses);

        return "cookbook";
    }

    @Autowired
    private CookbookCollectService cookbookCollectService;

    /**
     * 我的厨房
     * @return
     */
    @RequestMapping("/myKitchen")
    public String myKitchen(
            Model model,
            @RequestParam(value = "page", required = false, defaultValue = "1") int page) {
        if(LoginUtils.isSessionInvalid()) {
            return "/login";
        }
        Map<String, Object> map = cookbookCollectService.selectByCondition(page, Constants.pageSize, LoginUtils.getCurrentUserInfo().getId());
        List<Cookbook> cookbookList = (List<Cookbook>) map.get(Constants.ROWS);
        cookbookDetail(cookbookList);
        model.addAttribute("pageData", map);
        return "myKitchen";
    }

    /**
     * 搜索页面
     * @return
     */
    @RequestMapping("/search")
    public String search(
            Model model,
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "searchContent", required = false) String searchContent,
            @RequestParam(value = "orderBy", required = false) String orderBy) {
        Map<String, Object> map = cookbookService.selectPageByCondition(page, Constants.pageSize, null, searchContent, orderBy);
        List<Cookbook> cookbookList = (List<Cookbook>) map.get(Constants.ROWS);
        cookbookDetail(cookbookList);
        model.addAttribute("pageData", map);
        return "search";
    }

    /**
     * 发布食谱
     * @return
     */
    @RequestMapping("/publishCookbook")
    public String publishCookbook() {
        return "publishCookbook";
    }

    @Autowired
    private ShopCartService shopCartService;

    /**
     * 购买食谱
     * @return
     */
    @RequestMapping("/shoppingCart")
    public String shoppingCart(Model model) {

        List<Cookbook> cookbookList = new ArrayList<>();
        List<ShopCart> shopCarts = shopCartService.selectByCondition(LoginUtils.getCurrentUserInfo().getId(), null);
        for(ShopCart shopCart : shopCarts) {
            Integer cookbookId = shopCart.getCookbookid();
            Cookbook cookbook = cookbookService.selectByPrimaryKey(cookbookId);
            //食谱食材
            List<CookbookFood> cookbookFoods = cookbookFoodService.selectByCondition(cookbookId);
            for(int i = 0; i < cookbookFoods.size(); i++) {
                Food food = foodService.selectByPrimaryKey(cookbookFoods.get(i).getFoodid());
                cookbookFoods.get(i).setFood(food);
            }
            cookbook.setCookbookFoods(cookbookFoods);
            cookbookList.add(cookbook);
        }
        cookbookDetail(cookbookList);
        model.addAttribute("cookbookList", cookbookList);
        return "shoppingCart";
    }

}
